package com.wiim.ui;

import java.lang.Thread.UncaughtExceptionHandler;

import com.wiim.common.ConstantValue;
import com.wiim.common.Utils;
import com.wiim.handler.CMCmdHandler;
import com.wiim.interfaceapp.IServiceCommandResult;
import com.wiim.model.UserData;
import com.wiim.nhapnhap.AppGlobal;
import com.wiim.nhapnhap.EgoodApp;
import com.wiim.nhapnhap.R;
import com.wiim.tab.TabWidget;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements IServiceCommandResult {
		
	private Button btnLogin;
	private EditText edtEmail ,edtPassword;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout);			
		
		InsControls();
	}

	private void InsControls(){		
		
		btnLogin=(Button)findViewById(R.id.btnLogin);
		edtEmail=(EditText)findViewById(R.id.edtEmail);
		edtPassword=(EditText)findViewById(R.id.edtPassword);	
		
		// use for SDK android > 3.0
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectNetwork().build());
		
		GlobalExceptionHandler exceptionHandler = new GlobalExceptionHandler(Thread.getDefaultUncaughtExceptionHandler());
        Thread.setDefaultUncaughtExceptionHandler(exceptionHandler);
        
        btnLogin.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub				
		        UserData uData = new UserData();
				uData.Username = edtEmail.getText().toString().trim();
				uData.Password = edtPassword.getText().toString().trim();
				uData.DeviceID = Utils.getDeviceID(LoginActivity.this);	
				uData.AccountType = ConstantValue.AccountType.NATIVE.toString();
				
				Bundle b = new Bundle();
				b.putSerializable(EgoodApp.REST_OBJECT_PUT, uData);
				AppGlobal.getInstance().startServiceCmd(CMCmdHandler.COMMAND_LOGIN_VALIDATE, LoginActivity.this, new CMCmdHandler(), b,true);
			}
		});        
	}

	@Override
	public void onBackPressed() {
		System.exit(0);
	}


	public void processCommandResult(int commandCode, Object resultData) {
		Bundle bResultData = (Bundle) resultData;				
		switch (commandCode) {				
			case CMCmdHandler.COMMAND_LOGIN_VALIDATE:{
				UserData uData = (UserData)bResultData.getSerializable(EgoodApp.REST_OBJECT_RETURN);
				
				if(uData.ValidateUserResponse.equalsIgnoreCase(ConstantValue.ValidateUserResponse.SUCCESS.toString())){
					changeScreen();
				}else{						
					Toast.makeText(this,uData.ValidateUserResponse, Toast.LENGTH_LONG).show();
				}
			}	
				  break;

				default:
					break;
			}
	}
	
	private void changeScreen(){
		LoginActivity.this.finish();
		Intent itTab = new Intent().setClass(LoginActivity.this.getApplicationContext(), TabWidget.class);
		Bundle bundle = new Bundle();
		bundle.putInt("indexTab", 0);
		itTab.putExtras(bundle);
		LoginActivity.this.startActivity(itTab);
	}
	
	private class GlobalExceptionHandler implements UncaughtExceptionHandler {
		  // private UncaughtExceptionHandler oldHandler;
		  GlobalExceptionHandler(UncaughtExceptionHandler oldHandler) {
		   // this.oldHandler = oldHandler;
		  }
		  @Override
		  public void uncaughtException(Thread thread, Throwable throwable) {
		   // messageBox(MessageTypes.Info, throwable.getMessage());
		   //	SpellAppMgr.getInstance().setForegroundNFC(getApplicationContext(), false);
			  Log.e("uncaughtException", throwable.getMessage());
		  }
	 }
}